/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ZeroHora.DAO;

/**
 *
 * @author Gledson
 */
import br.com.cadastros.bean.Imovel;
import java.util.List;
import java.util.ArrayList;

public class imovelDAO extends DAO{
    public void cadastrar(Imovel i) throws Exception{
        openDatabase();
            String sql = "INSERT INTO imovel (logradouro, numero, complemento, bairro, municipio, referencia, valorAluguel, valorVenda, valorIPTU, descricao) VALUES (?,?,?,?,?,?,?,?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, i.getLogradouro());
            pstmt.setString(2, i.getNumero());
            pstmt.setString(3, i.getComplemento());
            pstmt.setString(4, i.getBairro());
            pstmt.setString(5, i.getMunicipio());
            pstmt.setString(6, i.getReferencia());            
            pstmt.setFloat(7, i.getValorAluguel());
            pstmt.setFloat(8, i.getValorVenda());
            pstmt.setFloat(9, i.getValorIPTU());
            pstmt.setString(10, i.getDescricao());
            pstmt.execute();
        closeDatabase();
    }
 public Imovel busca(Integer id) throws Exception{
        openDatabase();
        String sql = "select * from imovel where idImovel=?";
        pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, id);
        rs = pstmt.executeQuery();
        Imovel i = new Imovel();
        if(rs.next()){
            i.setIdImovel(rs.getInt("idImovel"));
            i.setLogradouro(rs.getString("logradouro"));
            i.setNumero(rs.getString("numero"));
            i.setComplemento(rs.getString("complemento"));
            i.setBairro(rs.getString("bairro"));
            i.setMunicipio(rs.getString("municipio"));
            i.setReferencia(rs.getString("referencia"));
            i.setValorAluguel(Float.parseFloat(rs.getString("valorAluguel")));
            i.setValorVenda(Float.parseFloat(rs.getString("valorVenda")));
            i.setValorIPTU(Float.parseFloat(rs.getString("valorIPTU")));
            i.setDescricao(rs.getString("descricao"));
        }
        return i;
    }
    
    public void delete(Imovel i)throws Exception{
        openDatabase();
        String sql = "DELETE from imovel WHERE idImovel = ?";
        pstmt = con.prepareStatement(sql);//Não é obrigatório, ele prepara o sql
        pstmt.setInt(1, i.getIdImovel());
        pstmt.execute();
    }
    
   public List<Imovel> listaImovel() throws Exception{
        openDatabase();
        String sql = "SELECT * FROM imovel";
        pstmt = con.prepareStatement(sql);
        rs = pstmt.executeQuery();
        Imovel i = null;
       List<Imovel> listaImovel = new ArrayList<Imovel>();
       while(rs.next()){
           i = new Imovel();
                    i.setIdImovel(rs.getInt("idImovel"));
                    i.setLogradouro(rs.getString("Logradouro"));
                    i.setNumero(rs.getString("numero"));
                    i.setComplemento(rs.getString("complemento"));
                    i.setBairro(rs.getString("bairro"));
                    i.setMunicipio(rs.getString("municipio"));
                    i.setReferencia(rs.getString("referencia"));
                    i.setValorAluguel(Float.parseFloat(rs.getString("valorAluguel")));
                    i.setValorVenda(Float.parseFloat(rs.getString("valorVenda")));
                    i.setValorIPTU(Float.parseFloat(rs.getString("valorIPTU")));
                    i.setDescricao (rs.getString("descricao"));                    
                   listaImovel.add(i);
       }           
       return listaImovel;        
    }
    
    public void update(Imovel i)throws Exception{
        openDatabase();
        String sql = "UPDATE imovel SET logradouro=?, numero=? , complemento=?, bairro=?, municipio=?, referencia=?, valorAluguel=?, valorVenda=?, valorIPTU=?, descricao=?" + 
                "WHERE idImovel=?";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, i.getLogradouro());
            pstmt.setString(2, i.getNumero());
            pstmt.setString(3, i.getComplemento());
            pstmt.setString(4, i.getBairro());
            pstmt.setString(5, i.getMunicipio());
            pstmt.setString(6, i.getReferencia());            
            pstmt.setFloat(7, i.getValorAluguel());
            pstmt.setFloat(8, i.getValorVenda());
            pstmt.setFloat(9, i.getValorIPTU());
            pstmt.setString(10, i.getDescricao());
            pstmt.setInt(11, i.getIdImovel());
       pstmt.executeUpdate();
        
    }
}

